package com.blb.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.Dept;
import com.blb.entity.LaserPrinter;
import com.blb.entity.PrinterDept;
import com.blb.service.IDeptService;
import com.blb.service.ILaserPrinterService;
import com.blb.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *生产管理的激光打印机管理
 * @author tb
 * @since 2020-05-20
 */
@Controller
@RequestMapping("/printer")
public class LaserPrinterController {

    @Autowired
    private ILaserPrinterService laserPrinterService;

    @Qualifier("deptServiceImpl")
    @Autowired
    private IDeptService deptService;
    //@RequiresPermissions("printer")
    @GetMapping("sc_dyj")
    public String printer(Model model){
        List<Dept> deptList = deptService.queryProduction(ShiroUtils.getCompany());
        model.addAttribute("deptList",deptList);
        return "production/sc_dyj";
    }
    //@RequiresPermissions("printer")
    @ResponseBody
    @GetMapping("page")
    public Map<String, Object> page(Integer draw, Integer start, Integer length){
        String company = ShiroUtils.getCompany();
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("iTotalRecords",laserPrinterService.getCount(company));
        pageMap.put("iTotalDisplayRecords",laserPrinterService.getCount(company));
        pageMap.put("data",laserPrinterService.Ilist(start, length,company));
        return pageMap;
    }
    //@RequiresPermissions("printer")
    @ResponseBody
    @PostMapping("search")
    public Map<String, Object> search(Integer start, Integer length,String lpNum){
        System.out.println(lpNum);
        Map<String, Object> pageMap = new HashMap<>();
        List<PrinterDept> search = laserPrinterService.search(lpNum,ShiroUtils.getCompany());
        pageMap.put("iTotalRecords",search.size());
        pageMap.put("iTotalDisplayRecords",search.size());
        pageMap.put("data",search);
        return pageMap;
    }


    //@RequiresPermissions("printer:add")
    @ResponseBody
    @PostMapping("/add")
    public Response add(LaserPrinter lp){
        lp.setId(UUIDUtils.getId());
        lp.setCompanyId(ShiroUtils.getCompany());
        laserPrinterService.save(lp);
        return new Response();
    }
    @RequiresPermissions("printer:update")
    @ResponseBody
    @PostMapping("/uppInfo")
    public Response uppInfo(String id){
        Response response = new Response();
        response.setData(laserPrinterService.getById(id));
        return response;
    }

    @RequiresPermissions("printer:update")
    @ResponseBody
    @PostMapping("/update")
    public Response update(PrinterDept lp){
        laserPrinterService.updateById(lp);
        return new Response();
    }

    @RequiresPermissions("printer:deleted")
    @ResponseBody
    @PostMapping("/delete")
    public Response delete(String id){
        laserPrinterService.removeById(id);
        return new Response();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequiresPermissions("printer:deleted")
    @ResponseBody
    @RequestMapping("/deletes")
    public Response delete(@RequestBody List<String> ids){
        boolean b = laserPrinterService.removeByIds(ids);
        if(b){
            return Response.success();
        }else{
            return Response.error("删除失败");
        }
    }

    /**
     * 查看打印机编号是否存在
     * @param lpNo
     * @return
     */
    @RequiresPermissions("printer")
    @ResponseBody
    @PostMapping("checkLpNo")
    public Response checkLpNo(String lpNo){
        Response res = new Response();
        res.setRs(laserPrinterService.checkLpNo(lpNo)>0);
        return res;
    }

}

